﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nature;
using Nature.Numerics;
using System.Diagnostics;
using Nature.Numerics.Basic;

namespace Nature.Toolkit.GEqu
{
    public class IdealGasChemicalEquilibriumCalculator : IChemicalEquilibriumCalculator
    {
        #region Private Fields
        TPIdealGasChemicalEquilibriumState m_tpState;
        TVIdealGasChemicalEquilibriumState m_tvState;
        HPIdealGasChemicalEquilibriumState m_hpState;
        UVIdealGasChemicalEquilibriumState m_uvState;
        #endregion

        public IdealGasChemicalEquilibriumCalculator(ModelObject model)
        {
            Model = model;
        }

        public ModelObject Model { get; private set; }

        #region IChemicalEquilibriumCalculator Members

        void IChemicalEquilibriumCalculator.TP(double temperature, double pressure, double[] x)
        {
            IChemicalEquilibriumState state = m_tpState =
                (m_tpState ?? new TPIdealGasChemicalEquilibriumState(this.Model));
            state.T = temperature;
            state.P = pressure;
            state.X = x;
            //-------------------
            state.X.CopyTo(x);
            state.DropState();
        }

        void IChemicalEquilibriumCalculator.TV(double temperature, ref double pressure, double[] x)
        {
            IChemicalEquilibriumState state = m_tvState =
                (m_tvState ?? new TVIdealGasChemicalEquilibriumState(this.Model));

            state.T = temperature;
            state.P = pressure;
            state.X = x;
            //-------------------
            pressure = state.P;
            state.X.CopyTo(x);
        }

        void IChemicalEquilibriumCalculator.HP(ref double temperature, double pressure, double[] x)
        {
            IChemicalEquilibriumState state = m_hpState =
                (m_hpState ?? new HPIdealGasChemicalEquilibriumState(this.Model));
            state.T = temperature;
            state.P = pressure;
            state.X = x;
            //-------------------
            temperature = state.T;
            state.X.CopyTo(x);
            state.DropState();
        }

        void IChemicalEquilibriumCalculator.UV(ref double temperature, ref double pressure, double[] x)
        {
            IChemicalEquilibriumState state = m_uvState =
                (m_uvState ?? new UVIdealGasChemicalEquilibriumState(this.Model));
            state.T = temperature;
            state.P = pressure;
            state.X = x;
            //-------------------
            temperature = state.T;
            pressure = state.P;
            state.X.CopyTo(x);
            state.DropState();
        }

        #endregion
    }
}